import os
import dearpygui.dearpygui as dpg

import src.date as date
from src.schedule_docx import Schedule
from src.schedule_tabs import ScheduleTabs


class MainWindow:
    def __init__(self, name="main"):
        self.schedule = Schedule()
        with dpg.theme() as theme:
            with dpg.theme_component(dpg.mvAll):
                dpg.add_theme_style(dpg.mvStyleVar_WindowRounding, 4)
                dpg.add_theme_style(dpg.mvStyleVar_ChildRounding, 2)
                dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 4)
                dpg.add_theme_style(dpg.mvStyleVar_PopupRounding, 4)
                dpg.add_theme_style(dpg.mvStyleVar_GrabRounding, 2)
            dpg.bind_theme(theme)
        with dpg.window(tag=name):
            dpg.set_primary_window(name, True)
            with dpg.file_dialog(
                label="导入",
                tag="load",
                show=False,
                modal=True,
                callback=self.load,
                width=700,
                height=400,
            ):
                dpg.add_file_extension(".docx")
            with dpg.file_dialog(
                label="导出",
                tag="save",
                show=False,
                modal=True,
                callback=self.save,
                width=700,
                height=400,
            ):
                dpg.add_file_extension(".docx")
            with dpg.group(horizontal=True):
                dpg.add_button(label="导入", callback=lambda: self.open_load())
                dpg.add_button(label="导出", callback=lambda: self.open_save())
                dpg.add_text(tag='info')
            self.tabs = ScheduleTabs(self.schedule)

    def load(self, sender, app_data, user_data):
        filename = app_data["file_path_name"]
        ret = self.schedule.load(filename)
        if ret:
            dpg.set_value('info', f'{ret}! {filename}')
            dpg.configure_item('info', color=(255, 0, 0, 255))
        else:
            dpg.set_value('info', filename)
            dpg.configure_item('info', color=(-255, 0, 0, 255))
            self.tabs.update()

    def save(self, sender, app_data, user_data):
        filename = app_data["file_path_name"]
        ret = self.schedule.save(filename)
        if ret:
            dpg.set_value('info', f'{ret}! {filename}')
            dpg.configure_item('info', color=(255, 0, 0, 255))
        else:
            dpg.set_value('info', filename)
            dpg.configure_item('info', color=(-255, 0, 0, 255))

    def open_load(self):
        dpg.show_item(f"load")
        name = os.path.splitext(os.path.basename(self.schedule.filename))[0]
        dpg.configure_item(f"load", default_filename=name)

    def open_save(self):
        dpg.show_item(f"save")
        name = os.path.splitext(os.path.basename(self.schedule.filename))[0]
        dpg.configure_item(f"save", default_filename=name)

if __name__ == "__main__":
    dpg.create_context()
    dpg.create_viewport(title="Schedule", width=800, height=600)

    with dpg.font_registry():
        with dpg.font("res/AlibabaPuHuiTi-3-55-Regular.ttf", 20) as font:
            dpg.add_font_range_hint(dpg.mvFontRangeHint_Default)
            dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Simplified_Common)
            dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Full)
            dpg.bind_font(font)
    win = MainWindow()

    dpg.setup_dearpygui()
    dpg.show_viewport()
    dpg.start_dearpygui()
    dpg.destroy_context()
